package com.rslai.base.tool.autotest.mock.util;

import com.rslai.commons.http.RslaiHttpClient;
import com.rslai.commons.http.data.HttpResult;

import java.io.IOException;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpUtil {
    private static final Logger logger = LoggerFactory.getLogger(HttpUtil.class);

    public static String post(String url, String data) {
        RslaiHttpClient httpClient = RslaiHttpClient.createDefaultClient();
        try {
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new StringEntity(data, "UTF-8"));
            HttpResult result = httpClient.httpExecute(httpPost);
            return result.getContent();
        } catch (IOException e) {
            logger.error("url:" + url + " " + e.getMessage(), e);
        }
        return "";
    }

    public static String get(String url, String data) {
        RslaiHttpClient httpClient = RslaiHttpClient.createDefaultClient();
        try {
            HttpGet httpGet = new HttpGet(url + "?" + data);
            HttpResult result = httpClient.httpExecute(httpGet);
            return result.getContent();
        } catch (IOException e) {
            logger.error("url:" + url + " " + e.getMessage(), e);
        }
        return "";
    }
}

